﻿using System;
using System.Linq;
using CTS.Com.Domain.Constants;
using CTS.Com.Domain.Helper;
using CTS.Data.Domain.Dao;
using CTS.W._150101.Models.Domain.Common.Dao;
using CTS.W._150101.Models.Domain.Object.Admin.Slides;
using CTS.Web.Com.Domain.Helper;

namespace CTS.W._150101.Models.Domain.Dao.Admin
{
    /// <summary>
    /// MasterSlidesEntryDao
    /// </summary>
    public class MasterSlidesEntryDao : GenericDao<EntitiesDataContext>
    {
        // Định nghĩa hằng file sql
        public const string MASTERSLIDESENTRYDAO_GETINFO_SQL = "MasterSlidesEntryDao_GetInfo.sql";

        /// <summary>
        /// Lấy thông tin slide
        /// </summary>
        public SlideObject GetInfo(string localeCd, string slideCd)
        {
            // Tạo tham số
            var param = new {
                LocaleCd = localeCd,
                SlideCd = slideCd
            };
            // Kết quả trả về
            return GetObjectByFile<SlideObject>(MASTERSLIDESENTRYDAO_GETINFO_SQL, param);
        }

        /// <summary>
        /// Kiểm tra tồn tại slide
        /// </summary>
        public bool IsExist(string localeCd, string slideCd)
        {
            var linq = from tbl in EntityManager.MASlides
                       where tbl.LocaleCd == localeCd
                       && tbl.SlideCd == slideCd
                       select tbl;
            var count = linq.Count();
            return count == decimal.One;
        }

        /// <summary>
        /// Kiểm tra tồn tại tên slide
        /// </summary>
        public bool IsUnique(string slideCd, string slideSearchName)
        {
            var linq = from tbl in EntityManager.MASlides
                       where tbl.SlideCd != slideCd
                       && tbl.SlideSearchName == slideSearchName
                       select tbl;
            var count = linq.Count();
            return count != decimal.One;
        }

        /// <summary>
        /// Thêm slide
        /// </summary>
        public void Insert(SlideObject data)
        {
            var entity = GetMASlide(data);
            entity.LocaleCd = Logics.LOCALE_DEFAULT;
            entity.VersionNo = decimal.One;
            entity.CreateUser = WebContextHelper.UserName;
            entity.CreateDate = DateTime.Now;
            entity.UpdateUser = WebContextHelper.UserName;
            entity.UpdateDate = DateTime.Now;
            EntityManager.MASlides.InsertOnSubmit(entity);
        }

        /// <summary>
        /// Cập nhật slide
        /// </summary>
        public void Update(SlideObject data)
        {
            var linq = from tbl in EntityManager.MASlides
                       where tbl.LocaleCd == data.LocaleCd
                       && tbl.SlideCd == data.SlideCd
                       select tbl;
            var entity = linq.Single();
            // Cập nhật thông tin
            if (!DataCheckHelper.IsNull(data.SlideName)) {
                entity.SlideName = data.SlideName;
            }
            if (!DataCheckHelper.IsNull(data.SlideSearchName)) {
                entity.SlideSearchName = data.SlideSearchName;
            }
            if (!DataCheckHelper.IsNull(data.FileCd)) {
                entity.FileCd = data.FileCd;
            }
            if (!DataCheckHelper.IsNull(data.SortKey)) {
                entity.SortKey = data.SortKey;
            }
            if (!DataCheckHelper.IsNull(data.DeleteFlag)) {
                entity.DeleteFlag = data.DeleteFlag;
            }
            entity.VersionNo = entity.VersionNo + decimal.One;
            entity.UpdateUser = WebContextHelper.UserName;
            entity.UpdateDate = DateTime.Now;
        }

        /// <summary>
        /// Khởi tạo đối tượng slide
        /// </summary>
        public MASlide GetMASlide(SlideObject data)
        {
            // Khởi tạo biến cục bộ
            var result = DataHelper.CopyObject<MASlide>(data);
            // Kết quả trả về
            return result;
        }
    }
}
